package com.ddm.config;



import com.ddm.interceptor.JwtTokenAdminInterceptor;
import com.ddm.interceptor.JwtTokenCommonInterceptor;
import com.ddm.interceptor.OpenIdTokenInterceptor;
import com.ddm.interceptor.WebTokenCommonInterceptor;
import com.ddm.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
    @Autowired
    private OpenIdTokenInterceptor openIdTokenInterceptor;
    @Autowired
    private JwtTokenCommonInterceptor jwtTokenCommonInterceptor;//废弃
    @Autowired
    private WebTokenCommonInterceptor webTokenCommonInterceptor;//废弃

    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        /** 解除注释 */
        //TODO
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/admin/login");


//        registry.addInterceptor(openIdTokenInterceptor)
//                .addPathPatterns("/user/**")
//                .excludePathPatterns("/user/user/login");

        List<String> excludePaths = new ArrayList<>();
// 基础登录路径
        excludePaths.add("/user/user/login");
        excludePaths.add("/Web/user/login");

// 新增白名单路径
        excludePaths.add("/bookmark/register-exam");
        excludePaths.add("/user/bonusponit/contest-statistics");
        excludePaths.add("/user/score/exam/{examId}");
        excludePaths.add("/user/contest-rank/{examId}/top");
        excludePaths.add("/user/contest");
        excludePaths.add("/user/contest/detail/{id}");
        excludePaths.add("/user/standardExam");

// 用户
        excludePaths.add("/user/bookmark/page");
        excludePaths.add("/user/bookmark/ifbookmark");
        excludePaths.add("/user/bookmark");
        excludePaths.add("/user/announcement");
        excludePaths.add("/user/user/phone");

// 通用
        excludePaths.add("/common/standard");

// 课程相关
        excludePaths.add("/Web/course/findByType");
        excludePaths.add("/Web/course/pageAll");
        excludePaths.add("/Web/course/findById");
        excludePaths.add("/Web/course/pageByUser");
        excludePaths.add("/Web/course/category/list"); // 补充：课程分类列表

// 资讯相关
        excludePaths.add("/Web/new/pageAll");
        excludePaths.add("/Web/new/findById");
        excludePaths.add("/Web/new/category/list"); // 补充：资讯分类列表

// 技术相关
        excludePaths.add("/Web/technique/pageAll");
        excludePaths.add("/Web/technique/findById");
        excludePaths.add("/Web/technique/hot/list"); // 补充：热门技术列表

// 法规相关
        excludePaths.add("/Web/regulation/pageAll");
        excludePaths.add("/Web/regulation/findById");
        excludePaths.add("/Web/regulation/pageByUserId");
        excludePaths.add("/Web/regulation/type/list"); // 补充：法规类型列表

// 市场分析相关
        excludePaths.add("/Web/market/An/TList");
        excludePaths.add("/Web/market/An/GById");
        excludePaths.add("/Web/market/An/pageByUserId");
        excludePaths.add("/Web/market/An/exportTemplate"); // 补充：导出模板

// 市场产品相关
        excludePaths.add("/Web/market/Sp/GList");
        excludePaths.add("/Web/market/Sp/GById");
        excludePaths.add("/Web/market/Sp/UGList");
        excludePaths.add("/Web/market/Sp/recommend/list"); // 补充：推荐产品列表
        excludePaths.add("/Web/market/Sp/RGList");

// 课堂相关
        excludePaths.add("/Web/CxClassroom/PList");
        excludePaths.add("/Web/CxClassroom/GById");
        excludePaths.add("/Web/CxClassroom/free/list"); // 补充：免费课程列表

// 标准文件相关
        excludePaths.add("/Web/standardFile/pageAll");
        excludePaths.add("/Web/standardFile/findById");
        excludePaths.add("/Web/standardFile/pageByUserId");
        excludePaths.add("/Web/standardFile/download/**"); // 补充：文件下载路径

// 用户相关
        excludePaths.add("/Web/user/info");
        excludePaths.add("/Web/user/agreement"); // 补充：用户协议
        excludePaths.add("/Web/user/privacy"); // 补充：隐私政策
        excludePaths.add("/Web/user/send-code");
        excludePaths.add("/Web/user/register");
        excludePaths.add("/Web/user/WebVlogin");
        excludePaths.add("/Web/user/ResetPassword");
        excludePaths.add("/Web/user/send-package");


        registry.addInterceptor(openIdTokenInterceptor)
                .addPathPatterns("/user/**")
                .addPathPatterns("/Web/**")
                .addPathPatterns("/common/downloadMAFile")//市场报告权限下载必须要拦截器
                .excludePathPatterns(excludePaths);

//        registry.addInterceptor(jwtTokenCommonInterceptor)
//                .addPathPatterns("/common/**");

//        registry.addInterceptor(webTokenCommonInterceptor)
//                .addPathPatterns("/Web/**")
//                .excludePathPatterns("/Web/user/login");


    }
    /**
     * 通过knife4j生成接口文档
     *
     * @return
     */
    @Bean
    public Docket docketAdmin() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("沉香种植项目接口文档")
                .version("1.0")
                .contact(new Contact("丁丁猫", "", "3324488076@qq.com"))
                .description("沉香种植项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("管理端接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ddm.controller.admin"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 通过knife4j生成接口文档
     *
     * @return
     */
    @Bean
    public Docket docketUser() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("沉香种植项目接口文档")
                .version("1.0")
                .contact(new Contact("丁丁猫", "", "3324488076@qq.com"))
                .description("沉香种植项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户端接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ddm.controller.user"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    /**
     * 通过knife4j生成接口文档
     *
     * @return
     */
    @Bean
    public Docket docketWxAdmin() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("沉香种植项目接口文档")
                .version("1.0")
                .contact(new Contact("丁丁猫", "", "3324488076@qq.com"))
                .description("沉香种植项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("微信管理端接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ddm.controller.wxadmin"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 通过knife4j生成接口文档
     *
     * @return
     */
    @Bean
    public Docket docketCommon() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("沉香种植项目接口文档")
                .version("1.0")
                .contact(new Contact("丁丁猫", "", "3324488076@qq.com"))
                .description("沉香种植项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("通用接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ddm.controller.common"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    /**
     * 设置静态资源映射
     *
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 扩展消息转换器
     * 用于统一格式
     * 日期格式
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("消息转换器...");
        //消息转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //对象转换器,把对象转换成json数据
        converter.setObjectMapper(new JacksonObjectMapper());
        //将消息转换器加入优先容器
        converters.add(0, converter);
    }
}
